<script setup>
import { ref, reactive, getCurrentInstance, watch } from "vue"
import { UserFilled } from '@element-plus/icons-vue'

const instance = getCurrentInstance();
const _this = instance.appContext.config.globalProperties //vue3获取当前this
const storage = _this.$utils.storage;

let header_active = storage.getLocal('header_active') || '1';
let menuActive = ref(header_active);

// 组件中使用，接收传参的
let propsData = defineProps({
	menu: {
		type: Array,
		default: [],
		required: true,
	},
	projectTitle: {
		type: String,
		default: ''
	},
	imgShow: {
		type: Boolean,
		default: false,
	},
	infoNum: {
		type: [Number, String],
		default: null
	},
	userName: {
		type: String,
		default: '',
	},
	userMenu: {
		type: Array,
		default: []
	},
})
// 组件中使用，接收事件的
let emit = defineEmits(['openAlarm']);

function open(){
	emit('openAlarm')
}

function gotoPage(path, id) {
  if (instance != null) {
      const _this = instance.appContext.config.globalProperties; //vue3获取当前this
      if (!path) console.error("请传路径地址");
      _this.$router.push({path, query: {}})
  }
}

</script>
<template>
<div class="header_nav">
	<div class="title">{{projectTitle}}</div>
	<el-menu
		class="menu_mw"
		background-color="var(--mw-page-background-bright-color)"
		text-color="var(--mw-not-important-color)"
		mode="horizontal"
		:ellipsis="false"
		active-text-color="var(--mw-tabs-menu-active-color)"
		:default-active="menuActive"
	>
		<template v-for="lv1 in menu">
			<el-sub-menu v-if="lv1.nodes && lv1.nodes.length > 0" :index="lv1.id" :key="lv1.id">
				<template #title><el-icon v-if="lv1.icon"><component :is="lv1.icon"></component></el-icon> <span>{{  lv1.title || '' }}</span> </template>
				<template v-for="lv2 in lv1.nodes">
					<el-sub-menu v-if="lv2.nodes && lv2.nodes.length > 0" :index="lv2.id" :key="lv2.id">
						<template #title><el-icon v-if="lv2.icon"><component :is="lv2.icon"></component></el-icon>{{  lv2.title || '' }}</template>
						<template v-for="lv3 in lv2.nodes" :key="lv3.id">
							<el-menu-item :index="lv3.id" @click="gotoPage(lv3.path, lv3.id)">
								<el-icon v-if="lv3.icon"><component :is="lv3.icon"></component></el-icon>
								<template #title>{{ lv3.title || ''}} </template>
							</el-menu-item>
						</template>
					</el-sub-menu>
					<el-menu-item v-else :index="lv2.id" :key="lv2.title" @click="gotoPage(lv2.path, lv2.id)">
						<el-icon v-if="lv2.icon"><component :is="lv2.icon"></component></el-icon>
						<template #title>{{ lv2.title || ''}} </template>
					</el-menu-item>
				</template>
			</el-sub-menu>
			<el-menu-item v-else :index="lv1.id" :key="lv1.title" @click="gotoPage(lv1.path, lv1.id)">
				<el-icon v-if="lv1.icon"><component :is="lv1.icon"></component></el-icon>
				<template #title>{{ lv1.title || ''}} </template>
			</el-menu-item>
		</template>
	</el-menu>
	<div class="author">
		<el-badge :value="infoNum" :max="99" class="alarm_info" v-if="infoNum" @click="open">
			<img class="alarm_icon" v-show="!imgShow" src="~@img/icon/header_ico_message@2x.png" alt="">
			<img class="alarm_icon" v-show="imgShow" src="~@img/icon/header_ico_message2.png" alt="">
		</el-badge>
		<img v-else class="alarm_icon" src="~@img/icon/header_ico_message@2x.png" alt="">
		<div class="author_name" @click="qwe">{{userName}}</div>
		<el-popover
			:width="154"
			popper-style="margin: 0 5px;box-shadow: rgb(14 18 22 / 35%) 0px 10px 38px -10px, rgb(14 18 22 / 20%) 0px 10px 20px -15px;"
		>
			<template #reference>
				<el-icon class="avatar"><UserFilled /></el-icon>
				<!-- <el-avatar :icon="UserFilled"  /> -->
			</template>
			<template #default>
				<div class="account_item" v-for="(item, index) in userMenu" :key="index">
					<el-button text @click="item.onClick">{{ item.label }}</el-button> 
				</div>
			</template>
		</el-popover>
	</div>
</div>
</template>

<style scoped lang="less">
.header_nav{
    display: flex;
    flex-flow: row;
		justify-content: space-between;
		padding: 0 20px;
		position: relative;
		.avatar{
			width: 32px;
			height: 32px;
			border-radius: 9999px;
			border: 1px solid #ccc;
			cursor: pointer;
		}
		.avatar:hover{
			background-color: #f5f7fa;
		}
		.title{
			width: fit-content;
			line-height: 60px;
			font-weight: 600;	
			color: #4c86f0;
			// color: #C7000B;
			
			// color: var(--mw-item-color-title);
			font-size: 24px;
			text-shadow: 2px 4px 5px #6593e6;
		}
		.menu_mw{
			width: fit-content;
			position: absolute;
			left: 50%;
			top: 0;
			transform: translateX(-50%);
		}
		.author{
			display: flex;
			align-items: center;
			color: #868A8D;
			.account_item{
				.el-button{
					width: 100%;
					text-align: left;
				}
			}
			.alarm_info{
				margin-right: 20px;
			}
			.alarm_icon{
				width: 20px;
				height: 20px;
				cursor: pointer;
			}
			.author_name{
				margin: 0 15px
			}
		}

}
</style>
